<?php
ob_start();
header("Content-type:text/html;charset=utf-8");
ini_set('memory_limit','88M');
ini_set('max_execution_time','0');
date_default_timezone_set('Asia/Shanghai');
error_reporting(0);
$date   = date('Y-m-d');
$dateex = explode('-',$date);
$stime  = time();
$stime1 = mktime('0','0','10',$dateex[1],$dateex[2],$dateex[0]);
$stime2 = mktime('23','59','59',$dateex[1],$dateex[2],$dateex[0]);
if($stime>=$stime1 && $stime<=$stime2){
}else{
	echo "fuck you mom!!!get out(return money)!! \n";
	exit;
}
include 'config.php';

/*----------链接mysql数据库  start------------*/
$conn = @mysql_connect($db_host,$db_user,$db_pwd) or die('mysql server failed!!');
@mysql_query('set names utf8',$conn);
@mysql_select_db($db_name,$conn) or die('can not find mysql dbname:'.$db_name);
/*----------链接mysql数据库  end------------*/

/*----------常用方法封装  start------------*/
function querys($sql){
	return @mysql_query($sql);
}

function getall($sql){
	$query = querys($sql);
	$list = '';
	while($row = @mysql_fetch_assoc($query)){
		$list[] = $row;
	}
	return $list;
}

function getone($sql){
	$query = querys($sql);
	return @mysql_fetch_assoc($query);
}
/*
*修改实例
*$table=表名
*$err=数组
*$id=编号
*/
function edit_update_cl($table,$err,$id){
	$sql = "UPDATE ".$table." SET ";
	foreach ( $err as $key => $value  ){
		if(preg_match("/^\d*$/",$key)){
			#过滤数字
		}else{
			$sql .= $key."='".addslashes($value)."', ";
		}
	}
	$sql = substr($sql, 0, strlen($sql)-2) .  " WHERE id=".$id;

	@mysql_query($sql) or die (mysql_error());

}

/*
*插入实例
*$table=表名
*$err=数组
*/
function add_insert_cl($table,$err){
	$sql = "INSERT INTO ".$table." (";
	foreach ( $err as $key => $value  ){
		$sql .= $key.", ";
	}
	$sql = substr($sql, 0, strlen($sql)-2) .  ") VALUES (";
	foreach ( $err as $key => $value  ){
		$sql .= "'".addslashes($value)."', ";
	}
	$sql = substr($sql, 0, strlen($sql)-2) .  ")";
	@mysql_query($sql) or die (mysql_error());
	return mysql_insert_id();
}

$sys=getone("select * from clt_system where id=1");
$xingqi = date("w");
if($sys['jingtai']=='close'){
	echo "静态已关闭";
	exit;
}
$timeday=strtotime(date("Y-m-d"));
if($timeday<=$sys['day_time']){
	echo "当日已发放";
	exit;
}



$time=unserialize($sys['jingtai_ff']);
if(in_array($xingqi, $time) || in_array(8, $time)) {
}else {
	echo "当前日期不能发放";
	exit;
}

/*修改或者新增bonus表*/
function edit_add_bonus($uid,$base_mey_per){
	/*修改bonus表,先查一下今天有没有数据*/
	$td_time = strtotime(date('Y-m-d'));
	$sql_check_bouns = "select * from clt_bonus where uid='".$uid."' and `createtime`>='".$td_time."'";
	$sql_check_bouns = getone($sql_check_bouns);
	if(!empty($sql_check_bouns['id'])){
		//如果有数据，直接修改对应的列
		$sql_bonus = "update clt_bonus set `b1`=(b1+$base_mey_per),`b0`=(b0+$base_mey_per) where id='".$sql_check_bouns['id']."'";
	}else{
		$sql_bonus = "insert into clt_bonus (is_type,uid,b0,b1,createtime) values(0,'".$uid."',$base_mey_per,$base_mey_per,'".time()."');";
	}
	querys($sql_bonus);
}

//统计每日奖金（日封顶基础）
function bonus_insert($b0,$uid,$b1,$b2,$b3,$b4,$b5,$b6,$b7=0,$b8=0,$b9=0,$b10=0,$b11=0,$b12=0,$b13=0) {
	$date=strtotime( date("Y-m-d"));
	$sql_bonus="select * from clt_bonus where createtime>".$date." and uid=".$uid;
	$bonus=getone($sql_bonus);
	if (!empty($bonus)){
		$bonus_update = NULL;
		$bonus_update ['b0'] = $bonus ['b0'] + $b0;
		$bonus_update ['b1'] = $bonus ['b1'] + $b1;
		$bonus_update ['b2'] = $bonus ['b2'] + $b2;
		$bonus_update ['b3'] = $bonus ['b3'] + $b3;
		$bonus_update ['b4'] = $bonus ['b4'] + $b4;//积分
		$bonus_update ['b5'] = $bonus ['b5'] + $b5;
		$bonus_update ['b6'] = $bonus ['b6'] + $b6;
		$bonus_update ['b7'] = $bonus ['b7'] + $b7;
		$bonus_update ['b8'] = $bonus ['b8'] + $b8;
		$bonus_update ['b9'] = $bonus ['b9'] + $b9;
		$bonus_update ['b10'] = $bonus ['b10'] + $b10;
		$bonus_update ['b11'] = $bonus ['b11'] + $b11;
		$bonus_update ['b12'] = $bonus ['b12'] + $b12;
		$bonus_update ['b13'] = $bonus ['b13'] + $b13;
		edit_update_cl ( 'clt_bonus', $bonus_update, $bonus ['id'] );

		/*	db('bonus')->where('id',$bonus['id'])->update($bonus_update);*/
		//edit_update_cl ( 'bonus', $bonus_update, $bonus ['id'] );
	} else {
		$bonus_update = NULL;
		$bonus_update ['createtime'] = time();
		$bonus_update ['uid'] = $uid;
		$bonus_update ['b0'] = $b0;
		$bonus_update ['b1'] = $b1;
		$bonus_update ['b2'] = $b2;
		$bonus_update ['b3'] = $b3;
		$bonus_update ['b4'] = $b4;
		$bonus_update ['b5'] = $b5;
		$bonus_update ['b6'] = $b6;
		$bonus_update ['b7'] = $b7;
		$bonus_update ['b8'] = $b8;
		$bonus_update ['b9'] = $b9;
		$bonus_update ['b10'] = $b10;
		$bonus_update ['b11'] = $b11;
		$bonus_update ['b12'] = $b12;
		$bonus_update ['b13'] = $b13;
		add_insert_cl ( 'clt_bonus', $bonus_update );
		//	db('bonus')->insert($bonus_update);
	}

}

//业绩统计
function achievement($a,$b,$c,$d,$e,$f,$g,$h,$i,$j=0,$k=0,$l=0,$m=0,$n=0){
	$createtime=strtotime(date('Y-m-d'));

	$achievement=getone("select * from clt_total_achievement where createtime=".$createtime." ");
	if(!empty($achievement)){
		$achievement_change['createtime']=$createtime;
		$achievement_change['newadd_achievement']=$achievement['newadd_achievement']+$a;
		$achievement_change['all_achievement']=$achievement['all_achievement']+$b;
		$achievement_change['newadd_user']=$achievement['newadd_user']+$c;
		$achievement_change['all_user']=$achievement['all_user']+$d;
		$achievement_change['new_grant']=$achievement['new_grant']+$e;
		$achievement_change['all_grant']=$achievement['all_grant']+$f;
		$achievement_change['bonus1']=$achievement['bonus1']+$g;
		$achievement_change['bonus2']=$achievement['bonus2']+$h;
		$achievement_change['bonus3']=$achievement['bonus3']+$i;
		$achievement_change['bonus4']=$achievement['bonus4']+$j;
		$achievement_change['bonus5']=$achievement['bonus5']+$k;
		$achievement_change['bonus6']=$achievement['bonus6']+$l;
		$achievement_change['bonus7']=$achievement['bonus7']+$m;
		$achievement_change['bonus8']=$achievement['bonus8']+$n;
		edit_update_cl ( 'clt_total_achievement', $achievement_change, $achievement['id'] );
		//	db('total_achievement')->where('id',$achievement['id'])->update($achievement_change);
	}else{
		$all_achievement=getone("select all_achievement from clt_total_achievement where id>0 order by id desc limit 0,1");
		$all_user=getone("select all_user from clt_total_achievement where id>0 order by id desc limit 0,1");
		$all_grant=getone("select all_grant from clt_total_achievement where id>0 order by id desc limit 0,1");
		$achievement_change['createtime']=$createtime;
		$achievement_change['newadd_achievement']=$a;
		$achievement_change['all_achievement']=$b+$all_achievement['all_achievement'];
		$achievement_change['newadd_user']=$c;
		$achievement_change['all_user']=$d+$all_user['all_user'];
		$achievement_change['new_grant']=$e;
		$achievement_change['all_grant']=$f+$all_grant['all_grant'];
		$achievement_change['bonus1']=$g;
		$achievement_change['bonus2']=$h;
		$achievement_change['bonus3']=$i;
		$achievement_change['bonus4']=$j;
		$achievement_change['bonus5']=$k;
		$achievement_change['bonus6']=$l;
		$achievement_change['bonus7']=$m;
		$achievement_change['bonus8']=$n;
		add_insert_cl ( 'clt_total_achievement', $achievement_change );
		//	db('total_achievement')->where('id',$achievement['id'])->insert($achievement_change);
	}
}

function money_log($id,$money,$beizhu,$jifen,$source){
	//增加积分变动日志
	$addinfo['type']=1;
	$addinfo['userid']=$id;
	$addinfo['dangqian']=$money+$jifen;
	$addinfo['num']="+".$money;
	$addinfo['source']=$source;
	$addinfo['time']=time();
	if(!empty($beizhu)){
		$addinfo['beizhu']=$beizhu;

	}
	add_insert_cl ( 'clt_money_log', $addinfo );
}




/*----------常用方法封装  end------------*/

//首先先获取需要执行静态的数据
$sql="select * from clt_users where money_max<money_qita";
$list = getall($sql);
if(empty($list)){
	echo '暂无需要执行静态的数据';
	exit;
}
/*
*	静态奖
*/
$sys=getone("select * from clt_system where id=1");
foreach($list as $k=>$v){
	$sql_level="select * from clt_user_level where `level`=".$v['level'];
	$level=getone($sql_level);
	$logs=getone("select count(*) as c from clt_money_log where source=12");
	$money=0;
	if($logs>0){
		$money=round($v['money_qian']*$level['b7']/100,2);
	}else{
		$money=round($v['money_qian']*$level['lirun']/100,2);
	}
	$beizhu = $v['nickname'];
	if($money>0){
		/*		$sql_dec="update clt_redelivery set maxmey=maxmey-".$money."  where id=".$v['id'];
                querys($sql_dec);*/

		querys("update clt_users set money_jifen=money_jifen+".$money .",money_qian=money_qian-".$money.",money_max=money_max+".$money."  where id=".$v['id']);

		money_log($v['id'],$money,$beizhu,$v['money_jifen'],12);

		bonus_insert($money,$v['id'],$money,0,0,0,0,0,0);

		achievement(0,0,0,0,$money,$money,0,0,0,$money,0);
	}
}
querys("update clt_system set day_time=".$timeday ." where id=1");
echo '发放成功';
?>



